package com.ruoyi.system.service.impl;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import org.springframework.stereotype.Service;

@Service
public class SendSmsImpl {

/**
 * 发送短信到指定电话号码
 *
 * @param workerName 工人姓名，用于填充短信模板
 * @param phoneNumber 接收短信的电话号码
 * @param projectName 工程项目名称，用于填充短信模板
 * @throws Exception 当发送短信过程中发生错误时抛出
 */
public void sendSms(String workerName, String phoneNumber, String projectName) throws Exception {
    // 创建阿里云短信服务客户端
    com.aliyun.dysmsapi20170525.Client client = Sample.createClient();
    // 拼接短信模板参数
    String templateParam = "{\"name\":\"" + workerName.trim() + "\",\"project\":\"" + projectName.trim() + "\"}";
    System.out.println(templateParam);
    // 构建发送短信的请求
    com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
            .setPhoneNumbers(phoneNumber)
            .setSignName("数智化工地管理系统")
            .setTemplateCode("SMS_465940383")
            .setTemplateParam(templateParam);
    // 设置运行时选项
    com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
    try {
        // 发送短信
        client.sendSmsWithOptions(sendSmsRequest, runtime);
    } catch (TeaException error) {
        // 处理阿里云SDK抛出的异常
        com.aliyun.teautil.Common.assertAsString(error.getMessage());
    } catch (Exception _error) {
        // 处理其他异常
        TeaException error = new TeaException(_error.getMessage(), _error);
        com.aliyun.teautil.Common.assertAsString(error.message);
    }
}

}

